﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using FitnessMVC.Data;

namespace FitnessMVCWeb.Helpers
{
    public class ValidUserConstraint : IRouteConstraint
    {
        IFitnessRepository data;

        public ValidUserConstraint()
            : this(new SqlFitnessRepository())
        {

        }

        public ValidUserConstraint(IFitnessRepository repo)
        {
            data = repo;
        }

        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            string passedInVal = values[parameterName].ToString();

            // TODO: some checking on the passed in val.

            var usr = data.GetUserByName(passedInVal);
            if (usr == null)
                return false;

            return true;
        }

        #endregion
    }
}
